package jmine.tec.services.export.impl;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import jmine.tec.services.api.ServicesAPIMessages;
import jmine.tec.services.api.export.EntityExporter;
import jmine.tec.services.api.export.EntityExporterRepository;
import jmine.tec.services.api.export.ExportException;
import jmine.tec.services.api.export.ExporterKey;

/**
 * Repositório de exportadores de entidades padrão.
 * 
 * @author lundberg
 */
public class DefaultEntityExporterRepository implements EntityExporterRepository {

    private final Map<ExporterKey, EntityExporter<?>> entityExporters = new ConcurrentHashMap<ExporterKey, EntityExporter<?>>();

    /**
     * {@inheritDoc}
     */
    public boolean hasExporterFor(ExporterKey key) {
        return this.entityExporters.containsKey(key);
    }

    /**
     * {@inheritDoc}
     */
    public EntityExporter<?> findEntityExporter(ExporterKey key) {
        if (!this.hasExporterFor(key)) {
            throw new ExportException(ServicesAPIMessages.EXPORTER_NOT_FOUND.create(key));
        } else {
            return this.entityExporters.get(key);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void register(Collection<? extends EntityExporter<?>> registers) {
        for (EntityExporter<?> entityExporter : registers) {
            this.entityExporters.put(entityExporter.getExporterKey(), entityExporter);
        }
    }
}
